package org.common.users.exception;

import org.codehaus.groovy.syntax.TokenException;
import org.common.users.entity.vo.ResponseData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理器
 *
 * @author samphin
 * @since 2020-7-18 09:11:28
 */
@RestControllerAdvice
public class ExceptionAspect {

    /**
     * Log4j日志处理
     */
    private static final Logger log = LoggerFactory.getLogger(ExceptionAspect.class);

    /**
     * 400 - 参数映射出错
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(HttpMessageNotReadableException.class)
    public ResponseData handleHttpMessageNotReadableException(
            HttpMessageNotReadableException e) {
        log.error("could_not_read_json...", e);
        return ResponseData.failure("could_not_read_json");
    }

    /**
     * 400 -参数校验失败
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler({MethodArgumentNotValidException.class})
    public ResponseData handleValidationException(MethodArgumentNotValidException e) {
        log.error("parameter_validation_exception...", e);
        return ResponseData.failure("parameter_validation_exception");
    }

    /**
     * 405 -请求方式不支持
     * 是ServletException的子类,需要Servlet API支持
     */
    @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public ResponseData handleHttpRequestMethodNotSupportedException(
            HttpRequestMethodNotSupportedException e) {
        log.error("request_method_not_supported...", e);
        return ResponseData.failure("request_method_not_supported");
    }

    /**
     * 415 - Unsupported Media Type。HttpMediaTypeNotSupportedException
     * 是ServletException的子类,需要Servlet API支持
     */
    @ResponseStatus(HttpStatus.UNSUPPORTED_MEDIA_TYPE)
    @ExceptionHandler({HttpMediaTypeNotSupportedException.class})
    public ResponseData handleHttpMediaTypeNotSupportedException(Exception e) {
        log.error("content_type_not_supported...", e);
        return ResponseData.failure("content_type_not_supported");
    }

    /**
     * 500 - Internal Server Error
     */
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(TokenException.class)
    public ResponseData handleTokenException(Exception e) {
        log.error("Token is invaild...", e);
        return ResponseData.failure("Token is invaild");
    }

    /**
     * 503 - 判断重复提交
     */
    @ExceptionHandler(RepeatRequestException.class)
    public ResponseData handleRepeatRequestException(Exception e) {
        log.error("请不要重复提交哦!!!!...", e);
        return ResponseData.failure(503,e.getMessage());
    }

    /**
     * 500 - Internal Server Error
     */
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(Exception.class)
    public ResponseData handleException(Exception e) {
        log.error("Internal Server Error...", e);
        return ResponseData.failure("Internal Server Error");
    }
}
